package com.cout970.magneticraft.systems.gui

import com.cout970.magneticraft.misc.gui.SlotType
import com.cout970.magneticraft.misc.inventory.isNotEmpty
import com.cout970.magneticraft.misc.network.IBD
import com.cout970.magneticraft.misc.t
import com.cout970.magneticraft.misc.vector.Vec2d
import com.cout970.magneticraft.misc.vector.vec2Of
import com.cout970.magneticraft.systems.config.Config
import com.cout970.magneticraft.systems.gui.components.bars.*
import com.cout970.magneticraft.features.TileBaseInserter
import net.minecraft.init.Items

fun GuiBuilder.inserterGui(tile: TileBaseInserter) {
    container {
        slot(tile.inventory, 0, "grabbed")
        slotGroup(3, 3, tile.filters, 0, "filters", SlotType.FILTER)
        region(0, 1) { stack, _ -> true }
        region(3, 9) { _, _ -> false }
        playerInventory()

        switchButtonState("btn0") { tile.inserterModule.whiteList }
        switchButtonState("btn1") { tile.inserterModule.useOreDictionary }
        switchButtonState("btn2") { tile.inserterModule.useMetadata }
        switchButtonState("btn3") { tile.inserterModule.useNbt }
        switchButtonState("btn4") { tile.inserterModule.canDropItems }
        switchButtonState("btn5") { tile.inserterModule.canGrabItems }

        onClick("btn0") { sendToServer(IBD().setInteger(0, 0)) }
        onClick("btn1") { sendToServer(IBD().setInteger(0, 1)) }
        onClick("btn2") { sendToServer(IBD().setInteger(0, 2)) }
        onClick("btn3") { sendToServer(IBD().setInteger(0, 3)) }
        onClick("btn4") { sendToServer(IBD().setInteger(0, 4)) }
        onClick("btn5") { sendToServer(IBD().setInteger(0, 5)) }

        receiveDataFromClient {
            it.getInteger(0) { prop ->
                val mod = tile.inserterModule
                when (prop) {
                    0 -> mod.whiteList = !mod.whiteList
                    1 -> mod.useOreDictionary = !mod.useOreDictionary
                    2 -> mod.useMetadata = !mod.useMetadata
                    3 -> mod.useNbt = !mod.useNbt
                    4 -> mod.canDropItems = !mod.canDropItems
                    5 -> mod.canGrabItems = !mod.canGrabItems
                }
            }
        }
    }

    bars {
        slotSpacer(3, 3)
        slotSpacer(1, 2)

        group(vec2Of(38, 58)) {
            switchButton("btn0", "btn0_offset", "btn0_on", "btn0_off", t("gui.magneticraft.inserter.btn0"), t("gui.magneticraft.inserter.btn0_off"))
            switchButton("btn1", "btn1_offset", "btn1_on", "btn1_off", t("gui.magneticraft.inserter.btn1"), t("gui.magneticraft.inserter.btn1_off"))
            switchButton("btn2", "btn2_offset", "btn2_on", "btn2_off", t("gui.magneticraft.inserter.btn2"), t("gui.magneticraft.inserter.btn2_off"))
            switchButton("btn3", "btn3_offset", "btn3_on", "btn3_off", t("gui.magneticraft.inserter.btn3"), t("gui.magneticraft.inserter.btn3_off"))
            switchButton("btn4", "btn4_offset", "btn4_on", "btn4_off", t("gui.magneticraft.inserter.btn4"), t("gui.magneticraft.inserter.btn4_off"))
            switchButton("btn5", "btn5_offset", "btn5_on", "btn5_off", t("gui.magneticraft.inserter.btn5"), t("gui.magneticraft.inserter.btn5_off"))
        }
    }
}
